package org.xp.judgeplatform.common;

import lombok.Data;

import java.util.List;

@Data
public class PageResult<T> {

    // 数据列表
    private List<T> records;

    // 总记录数
    private Long total;

    // 当前页码
    private Integer pageNum;

    // 每页大小
    private  Integer pageSize;

    // 总页数
    private Integer totalPages;

    // 是否有下一页
    private Boolean hasNext;

    // 是否有上一页
    private Boolean hasPrevious;

    public PageResult() {

    }

    // 有参构造
    public PageResult(List<T> records, Long total, Integer pageNum, Integer pageSize) {
        this.records = records;
        this.total = total;
        this.pageNum = pageNum;
        this.pageSize = pageSize;

        // 计算总页数
        this.totalPages = (int) Math.ceil((double) total / pageSize);

        // 计算是否有下一页
        this.hasNext = pageNum < totalPages;

        // 计算是否有上一页
        this.hasPrevious = pageNum > 1;
    }

    // 静态方法
    public static <T> PageResult<T> of(List<T> records, Long total, Integer pageNum, Integer pageSize){
        return new PageResult<>(records, total, pageNum, pageSize);
    }
}
